<?php

defined('IN_IA') or exit('Access Denied');
$table = 'sms_plan';
$dos =array_keys(getOperate());
$do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'list';

$status = getBizStatus();
if ($do == 'list') {
    $_W['page']['title'] = '管理监测通知 - 监测通知列表';

    $keyword = trim($_GPC['keyword']);

    $condition = ' ';
    if(! checkModuleRight($user, 'plan', 'audit',false)){
        $condition= " AND createid ={$_W['uid']} " ;
    }
    if (!empty($keyword)) {
        $condition .= " AND title LIKE '%{$keyword}%'";
        $_GET['keyword'] = $_GPC['keyword'];
    }
    if (isset($_GPC['searchtype'])&&($_GPC['searchtype']!='')) {
        $key = intval($_GPC['searchtype']);
        $condition = " WHERE status = " . $key . $condition;
        $_GET['searchtype'] = $key;
    } else {
        $condition = " WHERE status <" . DELETED . $condition;
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10; //ORDER BY createtime DESC
    $list = pdo_fetchall("SELECT * FROM " . tablename($table) . $condition . ' order by createtime desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($table) . $condition);
    $pager = pagination($total, $pindex, $psize);
    template('sms/plan_list');
}else{
    checkModuleRight($user,'plan',$do);
}
if ($do == 'add'||$do=='edit') {
    $_W['page']['title'] = '管理监测通知 - 增加监测通知';
    $url = url('sms/plan/' . $do);
    // 短信发送人
    // 短信发送模板
    $templates = pdo_fetchall('select * from ' . tablename('sms_template'), array(), 'id');
    $groups = getSendGroup();
    $id = intval($_GPC['id']);
    if ($id > 0) {
        $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = {$id}");
        $gid = $row['groupid'];
        checkRecodeRight($row, $user, 'edit');
    } else {
        $row = array('status'=>0);
        $gid = intval($_GPC['gid']);
        if ($gid == 0) {
            $gid = 1;
        }
        $plan = pdo_fetch("SELECT * FROM " . tablename('send_group') . "  WHERE id = {$gid}");
        $row['sendee'] = $plan['sendee'];
        $row['items'] = $plan['items'];
        unset($plan);
    }
    $values = json_decode($row['items'], true);
    $selects = json_decode($row['sendee'], true);

    if (checksubmit('submit')) {
        $data = array(
            'title' => trim($_GPC['title']),
            'creator' => $_W['username'],
            'createid' => $_W['uid'],
            'status' => 0
        );
        $content = array();
        foreach ($values as $k => $v) {
            $val = trim($_GPC['val_' . $k]);
            if (empty($val)) {
                message("请输入短信项目[{$v['title']}]的值！");
            } else {
                $values[$k]['value'] = $val;
            }
            $content[] = $v['title'] . ':' . $val;

        }
        $data['content'] = implode(',', $content);
        $data['items'] = json_encode($values);
        if ($_GPC['submit']=='audit') {
            $data['status'] = AUDIT;
        }
        if (empty($id)) {
            $data['groupid'] = $gid;
            $data['sendee'] = $row['sendee'];
            $data['changetime'] = TIMESTAMP;
            $data['createtime'] = TIMESTAMP;
            pdo_insert($table, $data);
        } else {
            $data['changetime'] = TIMESTAMP;
            pdo_update($table, $data, array('id' => $id));
        }
        message('数据更新成功！', url('plan/plan/list'), 'success');
    }
    template('sms/plan_post');
}
if ($do == 'audit') {

    $id = intval($_GPC['id']);
    $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE id = {$id}");
    if (empty($row)||$row['status']!=AUDIT) {
        message('数据不存在或非提交审核状态！');
    } else {
        checkRecodeRight($row, $user, 'audit');
    }
    if (checksubmit('submit')) {
        if ($_GPC['submit'] == 'pass') {
            $status = PASS;
        } else {
            $status = NOPASS;
        }
        $data = array(
            'status' => $status,
            'auditor' => $_W['username'],
            'auditid' => $_W['uid'],
            'changetime' => TIMESTAMP,
        );
        pdo_update($table, $data, array('id' => $id));
        AddSMSMsg($row, 'grant');
        message('数据更新成功！', url('plan/plan/list'), 'success');
    }
    $values = json_decode($row['items'], true);
    $selects = json_decode($row['sendee'], true);
    template('sms/plan_audit');
}
if ($do == 'delete') {
    checkModuleRight($user,'plan','delete');
    $id = intval($_GPC['id']);
    if ($id > 0) {
        $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = {$id}");
        if (!in_array($row['status'], array(PASS, AUDIT))) {
            pdo_update($table, array('status' => DELETED, 'changetime' => TIMESTAMP), array('id' => $row['id']));
        }
        message('数据删除成功！', url('plan/plan/list'), 'success');
    }
}
if($do == 'disp'){
    $id = intval($_GPC['id']);
    $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE id = {$id}");
    $values = json_decode($row['items'], true);
    $selects = json_decode($row['sendee'], true);
    template('sms/plan_display');
}